﻿String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

if(document.location.href != 'about:blank' && window==window.top && !document.location.href.startsWith('safari-extension://'))
{
var m_exclude_sites='';
var m_block_sites='';
var m_bad_keywords='';
var m_strict_mode='';
var m_urlblacklisted='';

var page_html='';

var LoadingPage = "<div id='cover' style='cursor: progress; position:absolute; top:0; left:0; right:0; bottom:0; z-index:99;'></div>";
/// Trim Function

function trim(str){
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


			
function handleijs(response)
{
//console.log('handleijs('+response.name+')');

	if(response.name == 'access')
	{
		var m_action=response.message.action;			
		if(m_action == 'stop')
		{
				window.stop(); 			
				document.location.href=safari.extension.baseURI+'error.html?'+document.location.href;
				return;
		}
		else if(m_action == 'allow')
		{			
				//document.getElementsByTagName('html')[0].innerHTML=page_html;
				document.body.innerHTML=page_html;
				return;
		}
	}else if(response.name == 'main')
	{
				m_exclude_sites="antiporn.felsoft.com.au,"+response.message.exclude_sites;
				m_block_sites=response.message.block_sites;
				m_bad_keywords=response.message.bad_keywords;
				m_strict_mode=response.message.strict_mode;
				m_urlblacklisted=response.message.urlblacklisted;
						
		if(m_urlblacklisted == 'stop')
		{
			window.stop(); 			
			document.location.href=safari.extension.baseURI+'error.html?'+document.location.href;
			return;
		}

						
			var keywords = m_bad_keywords.split(',');
			var exclude_sites = m_exclude_sites.split(",");
			var block_sites = m_block_sites.split(",");
			
		// For some reason, no script is displayed. This is just to avoid this noframes display.
		try{
		document.getElementsByTagName('noscript')[0].innerHTML='';
		}catch(err){}



		page_html=document.getElementsByTagName('html')[0].innerHTML;
		var loc=location.href;
		if(loc.length>66)
			loc=loc.substring(0, 63)+"...";	
			
			var i;
			var flag=false;
					var urlhost=location.href.toLowerCase();
			
					for(i in exclude_sites)
					{
						if(urlhost.indexOf(trim(exclude_sites[i]))!=-1 && trim(exclude_sites[i]) != '')
						{
							// If found, don't filter
							flag=true;
							break;
						}
					}
			
			if(flag == false)
			{
			 if(m_strict_mode == '1')
			 {
					window.stop(); 			
					document.location.href=safari.extension.baseURI+'error.html?'+document.location.href;
					return;
			 }
			 else
			 {
				for(i in block_sites)
				{
				
					if(urlhost.indexOf(trim(block_sites[i]))!=-1 && trim(block_sites[i]) != '')
					{	
						// If found, filter them					
						window.stop(); 			
						document.location.href=safari.extension.baseURI+'error.html?'+document.location.href;
						return;
					}
				}	
				for(i in keywords)
				{
					if(page_html.toLowerCase().indexOf(' '+keywords[i].toLowerCase())!=-1 || urlhost.indexOf(' '+keywords[i].toLowerCase())!=-1)
					{
						//////////////////////
						ap_settings = new Object();			
						ap_settings.req_url = document.location.href;
						//console.log('Seeking permission...');
						
						//var LoadingPage_tmp = LoadingPage.replace(/@URL/gi,loc);
						//document.getElementsByTagName('html')[0].innerHTML=LoadingPage;
						document.body.innerHTML=LoadingPage;
						
						safari.self.tab.dispatchMessage("ext_load", ap_settings);						
						//////////////////////
						break;
					}
				}
			}
		}
	}
}

safari.self.addEventListener("message", handleijs, false);

ap_settings = new Object();
ap_settings.req_url = document.location.href;

//console.log('Dispatching message to background.html');
safari.self.tab.dispatchMessage("settings", ap_settings);
}
